package com.maxcion.scalelayoutmanager.adapter

import android.util.SparseIntArray
import android.view.ViewGroup


/**
 * 如果要使用recyclerView的多布局  就继承自这个累,只用实现convert(BaseViewHolder holder, T item)
 *
 *
 * 数据源bean 必须实现 IMultiItem 接口
 *
 *
 *
 * 并在构造器中 进行 item type 与布局的绑定  通过 addItemLayout(int itemType,  int layoutId)
 *
 * 类似下面的代码
 * public SelectedAdapter(List<FinalSelectedBean> dataList) {
 * super(dataList);
 *
 * addItemLayout(FinalSelectedBean.TYPE_TITLE, R.layout.item_selected_title_type);
 * addItemLayout(FinalSelectedBean.TYPE_SINGLE_SELECTED, R.layout.item_selected_content);
 * addItemLayout(FinalSelectedBean.TYPE_MULTIPLE_SELECTED, R.layout.item_selected_content);
 * }
 *
 *
 *
</FinalSelectedBean> */
abstract class PageLoadMultiRecyclerViewAdapter<T : IMultiItem?, V : BaseViewHolder?>(dataList: List<T>?) :
    PageLoadRecyclerVewAdapter<T>(dataList) {
    private var mItemLayouts: SparseIntArray? = null
    override val itemLayoutId: Int
        protected get() = 0

    protected fun addItemLayout(itemType: Int, layoutId: Int) {
        if (mItemLayouts == null) {
            mItemLayouts = SparseIntArray()
        }
        mItemLayouts!!.append(itemType, layoutId)
    }

    override fun getBaseViewHolderByType(parent: ViewGroup, viewType: Int): BaseViewHolder {
        var result: BaseViewHolder? = null
        if (mItemLayouts != null) {
            result = BaseViewHolder.getHolder(
                parent.context, parent,
                mItemLayouts!![viewType]
            )
        }
        return result!!
    }

    override fun getDefItemViewType(position: Int): Int {
        return mDataList!![position]!!.getItemType()
    }
}